package fr.afcepf.atod21.brigade.daoImpl;

import java.util.List;

import javax.persistence.EntityManager;

import fr.afcepf.atod21.brigade.dao.BasicDaoImpl;
import fr.afcepf.atod21.brigade.entity.Brigadier;

public class BrigadierDaoJpa extends BasicDaoImpl<Brigadier> {

	@Override
	public void persist(Brigadier anEntity) {
		EntityManager em = getEm();
		em.getTransaction().begin();
		em.persist(anEntity);
		em.getTransaction().commit();
	}

	@Override
	public Brigadier merge(Brigadier anEntity) {
		EntityManager em = getEm();
		em.getTransaction().begin();
		Brigadier brigadier = em.merge(anEntity);
		em.getTransaction().commit();
		return brigadier;
	}

	@Override
	public void remove(Brigadier anEntity) {
		EntityManager em = getEm();
		em.getTransaction().begin();
		em.remove(anEntity);
		em.getTransaction().commit();
	}

	@Override
	public Brigadier read(int id) {
		return (Brigadier) getEm().createQuery("select b from Brigadier b where b.idBrigadier = :paramId").setParameter("paramId", id).getSingleResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Brigadier> findAll() {
		return getEm().createQuery("select b from Brigadier b").getResultList();
	}
}
